using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;
using HtmlAgilityPack;
using System.Text;


/// <summary>
/// Summary description for Calculator
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Calculator : System.Web.Services.WebService {

    private HtmlWeb hw = new HtmlWeb();
    private HtmlDocument doc;
    private HtmlNodeCollection hn;
    private HtmlNodeCollection hn1;
    private HtmlNodeCollection nodes;
    private HtmlNode node;
    
    [WebMethod]
    public string topNewsHome()
    {
        doc = hw.Load("http://vnexpress.net/GL/Home/");
        hn = doc.DocumentNode.SelectNodes("//*[@class='img-topnews fl']");
        hn1 = doc.DocumentNode.SelectNodes("//*[@class='link-topnews']");
        StringBuilder sb = new StringBuilder();
        try
        {
            foreach (HtmlNode item in hn1)
            {
                sb.Append("<p><a href='DetailNews.aspx?Link=" + item.Attributes["href"].Value + "'>");
                foreach (HtmlNode item1 in hn)
                {
                    sb.Append("<img src='http://vnexpress.net" + item1.Attributes["src"].Value + "' alt='' /></a>");
                    foreach (HtmlNode item2 in hn1)
                    {
                        sb.Append("<br/><a href='DetailNews.aspx?Link=" + item2.Attributes["href"].Value + "'>" + item2.InnerHtml + "</a>" + "</p");
                    }
                }
            }
            
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        return sb.ToString();
    }

    [WebMethod]
    public string topListNewsHome()
    {
        doc = hw.Load("http://vnexpress.net/GL/Home/");
        hn = doc.DocumentNode.SelectNodes("//*[@class='link-toplist']");
        StringBuilder sb = new StringBuilder();
        try
        {
            foreach (HtmlNode item in hn)
            {
                sb.Append("<li><a href='DetailNews.aspx?Link=" + item.Attributes["href"].Value + "'>" + item.InnerHtml + "</a>" + "<br/></li>");
            }

        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        return sb.ToString();
    }

    [WebMethod]
    public string listNewsHome()
    {
        doc = hw.Load("http://vnexpress.net/GL/Home/");
        hn = doc.DocumentNode.SelectNodes("//*[@class='link-title']");
        StringBuilder sb = new StringBuilder();
        try
        {
            foreach (HtmlNode item in hn)
            {
                sb.Append("<li><a href='DetailNews.aspx?Link=" + item.Attributes["href"].Value + "'>" + item.InnerHtml + "</a>" + "<br/></li>");
            }

        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        return sb.ToString();
    }


    [WebMethod]
    public string detailNews(string link)
    {
            doc = hw.Load("http://vnexpress.net" + link);
            nodes = doc.DocumentNode.SelectNodes("//img");
            node = doc.DocumentNode.SelectSingleNode("//title");
            foreach (HtmlNode item in nodes)
            {
                HtmlAttribute ha = item.Attributes["src"];
                item.Attributes["src"].Value = "http://vnexpress.net" + ha.Value;
            }
            return doc.DocumentNode.SelectSingleNode("//*[@id='content']").ChildNodes[1].ChildNodes[5].InnerHtml;
    }

    [WebMethod]
    [return : XmlElement (ElementName="Calculator")]
    public int cong( int a, int b)
    {
        int result = (a + b);
        return result;
    }

    [WebMethod]
    [return: XmlElement(ElementName = "Calculator")]
    public double chia(int a, int b)
    {
        int result = (a/b);
        return result;
    }
}

